<template>
	<view class="main">
		<!-- #ifndef MP-TOUTIAO -->
		<!-- <nav-bar ref="navRef" title="支付"></nav-bar> -->
		<!-- #endif -->
		<button @tap.stop="exportExcel">下载</button>
	</view>
</template>

<script>
export default {
	data() {
		return {};
	},
	onLoad() {},
	methods: {
		exportExcel() {
			let header = {};
			header["X-Access-Token"] =
				"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxNjA1MDUwNDU3NjQ2NDk3Nzk0Iiwicm5TdHIiOiJlcnRIZGU1UzRpaFIxaEVEcHBlbWU3NHpWSVUwbmhFNSIsInVzZXJpZCI6IjE2MDUwNTA0NTc2NDY0OTc3OTQiLCJ1c2VybmFtZSI6IjEzODAzNTc2NDc2IiwicmVhbG5hbWUiOiLmtYvor5VRIiwiZXhwIjoxNzU0NTM2NzAwfQ.DcjlFS2tM665ElqFBFRVsy2LlNZ4q603QnUZ3jm7cfA";
			uni
				.request({
					url: "https://api-test.jinzhuangli.com/app/as/order/schemeList/export/1942772748760121344",
					method: "POST",
					header: header,
					responseType: "arraybuffer"
				})
				.then((res) => {
					console.log("res>>>", res);
					const now = new Date();
					// console.log(res)
					const fileName = now.toISOString() + ".xlsx";
					const arrayBuffer = res[1].data; // utf-8编码的文件数据
					console.log("arrayBuff", arrayBuffer);
					const base64String = uni.arrayBufferToBase64(arrayBuffer);
					console.log("base64String", base64String);
					const buffer = uni.base64ToArrayBuffer(base64String);
					console.log("buffer", buffer);
					let fs = uni.getFileSystemManager();
					console.log("fs", fs);
					fs.getSavedFileList({
						success(res) {
							console.log("已保存的", res);
						}
					});
					const filePath = wx.env.USER_DATA_PATH + "/" + fileName;
					console.log("filePath", filePath);
					const deviceInfo = wx.getDeviceInfo();
					fs.writeFile({
						filePath: filePath,
						data: buffer,
						encoding: "binary",
						success: (res) => {
							console.log("文件保存成功", res);
							uni.openDocument({
								filePath: filePath,
								showMenu: true,
								success: (res) => {
									console.log("文件预览成功");
									// 构建分享内容
								},
								fail: (error) => {
									console.error("文件保存失败", error);
								}
							});
						}
					});
				});
		}
	}
};
</script>

<style lang="less" scoped>
.main {
	width: 100vw;
	min-height: 100vh;
	background-color: #f7f7f7;
	padding-bottom: calc(24rpx + env(safe-area-inset-bottom) / 2);
}
</style>
